package cn.istartfinancial.service.flowable.impl;


import cn.istartfinancial.entity.flowable.ActAssignee;
import cn.istartfinancial.mapper.flowable.ActAssigneeMapper;
import cn.istartfinancial.service.flowable.ActAssigneeService;
import cn.istartfinancial.util.UUIDUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.converter.BpmnXMLConverter;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.SubProcess;
import org.flowable.bpmn.model.UserTask;
import org.flowable.common.engine.impl.util.io.InputStreamSource;
import org.flowable.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;

/**
 * @author zhuxiaomeng
 * @date 2018/1/23.
 * @email 154040976@qq.com
 */
@Service
public class ActAssigneeServiceImpl implements ActAssigneeService
{

    @Autowired
    ActAssigneeMapper actAssigneeMapper;

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public int deleteByNodeId(String nodeId)
    {
        return actAssigneeMapper.deleteByNodeId(nodeId);
    }

    @Override
    public List<FlowElement> getActivityList(String deploymentId)
    {
        org.flowable.engine.repository.ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();
        InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(),
                processDefinition.getResourceName());
        BpmnModel bm = new BpmnXMLConverter()
                .convertToBpmnModel(new InputStreamSource(inputStream), false, true);
        List<Process> processes = bm.getProcesses();
        List<FlowElement> flowElements = Lists.newArrayList();
        processes.forEach(process -> {
            flowElements.addAll(process.findFlowElementsOfType(UserTask.class));
            flowElements.addAll(process.findFlowElementsOfType(SubProcess.class));
        });
        return flowElements;

    }

    @Override
    public void selectAllActivity(Collection<FlowElement> parentElements, List<FlowElement> flowElements)
    {
        for (FlowElement flowElement : parentElements)
        {
            if (flowElement instanceof UserTask)
            {
                SubProcess subProcess = flowElement.getSubProcess();
                if (null != subProcess)
                {
                    selectAllActivity(subProcess.getFlowElements(), flowElements);
                }
                flowElements.add(flowElement);
            }
        }
    }

    @Override
    public List<ActAssignee> select(ActAssignee actAssignee)
    {
        return actAssigneeMapper.select(actAssignee);
    }

    @Override
    public int insertSelective(ActAssignee actAssignee)
    {
        if (null == actAssignee.getId() ||
                StringUtils.isEmpty(actAssignee.getId()))
        {
            actAssignee.setId(UUIDUtils.getUUID());
        }
        return actAssigneeMapper.insertSelective(actAssignee);
    }
}
